<template>
  <div class="container" v-if="isShow">
    <div class="song-more" @click.stop="closeModel">
      <div class="main-mes" @touchmove.prevent @click.stop="mianBox">
        <div class="song-mes">
          <div class="left">
            <img :src="this.musicObject.background" alt />
          </div>
          <div class="right">
            <div class="song-name fs-36">{{this.musicObject.music_name}}</div>
            <div class="singer-name fs-26">{{this.musicObject.singer_name}}</div>
          </div>
        </div>

        <!-- 选项列表 -->
        <div
          class="option-list"
          v-for="(item,index) in optionList"
          :key="index"
          @click.stop="optionCheck(item.id)"
        >
          <div class="items">
            <div class="left">
              <div class="img">
                <img :src="item.img" alt />
              </div>
              <div class="text">{{item.text}}</div>
              <div class="vip" v-show="item.id == 3">
                <img :src="vip" alt />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 收藏到歌单模态框 -->
    <div
      class="collectModel"
      @touchmove.prevent
      @click.stop="closeCollectModel"
      v-show="isShowCollect"
    >
      <div class="add-box">
        <div class="add">
          <div class="img" @click.stop="addSheet">
            <img :src="add" alt />
          </div>
          <span class="text">新建歌单</span>
        </div>
        <div class="list">
          <div
            class="list-item"
            v-for="(item,index) in list"
            :key="index"
            @click="chooseSheet(item.song_sheet_id)"
          >
            <div class="center-col">
              <div class="left center-col">
                <img :src="item.song_sheet_photo" alt />
              </div>

              <div class="right flex-column">
                <p>{{item.song_sheet_name}}</p>
                <p class="color-9">{{item.music_num}}首</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 新建歌单 -->
    <div class="addmusic-list" @touchmove.prevent v-if="addshow">
      <div class="contents bg-f">
        <div class="top padding-24 space-between">
          <p class="fs-32 color-6" @click="cancel">取消</p>
          <p class="fs-32">新建歌单</p>
          <p class="fs-32 color-6" @click="sure">确定</p>
        </div>
        <div class="setmusic padding-24">
          <input type="text" placeholder="歌单名称" class="fs-32 color-6" v-model="songname" />
          <div class="center-col">
            <div class="yuan center bg-f" :class="{active:ac}" @click="checkStatus">
              <p></p>
            </div>
            <p class="fs-32 color-6">设置为隐私歌单</p>
          </div>
        </div>
      </div>
    </div>
    <shareModel v-show="leaderModal" @fn="getShareModel" :leaderModal="leaderModal"></shareModel>
  </div>
</template>

<script>
export default {
  name: "songMore",
  inject: ["reload"],
  data() {
    return {
      store: this.getStore(),
      deleteMore: require("@/assets/index/delete.png"),
      vip: require("@/assets/index/vip.png"),
      add: require("@/assets/mine/add.png"),
      title: "",
      isShow: false, //歌曲更多模态框
      isShowCollect: false, //添加到歌单模态框
      addshow: false, //新增歌单
      optionList: [
        {
          id: 1,
          img: require("@/assets/index/song-more-play.png"),
          text: "下一首播放"
        },
        {
          id: 2,
          img: require("@/assets/index/song-more-add.png"),
          text: "收藏到歌单"
        },
        {
          id: 3,
          img: require("@/assets/index/song-more-download.png"),
          text: "下载"
        },
        {
          id: 4,
          img: require("@/assets/index/song-more-chat.png"),
          text: "评论（120）"
        },
        {
          id: 5,
          img: require("@/assets/index/song-more-share.png"),
          text: "分享"
        },
        {
          id: 6,
          img: require("@/assets/index/song-more-user.png"),
          text: "歌手："
        },
        {
          id: 7,
          img: require("@/assets/index/song-more-album.png"),
          text: "专辑"
        },
        {
          id: 8,
          img: require("@/assets/index/delete.png"),
          text: "删除"
        }
      ],
      checkedList: [],
      playingList: [],
      list: [], //添加到歌单 模态框内歌单列表
      userinfo: "", //用户信息
      musicIdList: [], //选中的音乐id数组
      ac: false, //控制是否是隐私歌单的样式
      songname: "", //新增的歌单名称
      leaderModal: false //控制微信内分享模态框
    };
  },
  watch: {
    showSongMore: function(val) {
      this.isShow = val;
    },
    musicObject: function(val) {
      console.log(val.comment_num, "---val.comment_num");
      if (val.singer_name) {
        this.optionList[5].text = "歌手:" + " " + val.singer_name;
      } else if (val.singer_artistic_name) {
        this.optionList[5].text = "歌手:" + " " + val.singer_artistic_name;
      }
      if (val.comment_num) {
        this.optionList[3].text = "评论:" + " " + "(" + val.comment_num + ")";
      } else {
        this.optionList[3].text = "评论:" + " " + "(" + 0 + ")";
      }
      if (val.music_list_name) {
        this.optionList[6].text = "专辑:" + " " + val.music_list_name;
      } else {
        this.optionList[6].text = "专辑:";
      }
    }
  },
  props: {
    showSongMore: Boolean,
    musicObject: Object
  },
  components: {},
  methods: {
    // 获取分享模态框传过来的status
    getShareModel(e) {
      this.leaderModal = e;
    },
    // 切换是否设置为隐私歌单
    checkStatus() {
      this.ac = !this.ac;
    },
    // 确定新增歌单
    sure() {
      if (this.songname == "") {
        this.AlertWin("请输入歌单名");
        return;
      }
      var pe = 2;
      if (this.ac == true) {
        pe = 1;
      } else {
        pe = 2;
      }
      var url = this.$host1 + `home/addSongSheet`;
      var data = {
        song_sheet_name: this.songname,
        is_personal: pe
      };
      this.$axios
        .post(url, data)
        .then(res => {
          console.log(res);
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.addshow = false;
              this.songname = "";
              this.addshow = false;
              this.getUserSongSheetList(); //重新获取下用户歌单列表数据
              setTimeout(() => {
                this.AlertWin("添加成功");
              }, 500);
            } else {
              this.AlertWin(resData.message);
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 取消新建歌单
    cancel() {
      this.songname = "";
      this.addshow = false;
    },
    // 选择歌单添加歌曲
    chooseSheet(id) {
      console.log("选择的是----" + id);
      this.musicIdList.push(this.musicObject.music_id);
      this.addSongSheet(id);
    },
    // 批量添加到歌单
    addSongSheet(id) {
      const url = this.$host1 + `home/collectSomeSongSheet`;
      var params = {
        song_sheet_id: id,
        music_id: this.musicIdList
      };
      this.$axios
        .post(url, params)
        .then(res => {
          console.log(res.data, "---res.data--");
          if (res.data.code == 200) {
            this.AlertWin("添加成功");
            this.musicIdList = [];
            this.getUserSongSheetList(); //重新获取一下歌单的数据
          } else {
            this.musicIdList = [];
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 显示新增歌单模态框
    addSheet() {
      this.addshow = true;
    },
    // 获取用户信息
    getUserInfo() {
      var url = this.$host1 + `home/getPersonalMsg`;
      this.$axios
        .get(url)
        .then(res => {
          //console.log(res)
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.userinfo = resData.data;
              this.getUserSongSheetList();
            } else if (resData.code == 0) {
              // this.$router.push("/login");
            } else {
              this.AlertWin(resData.message);
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 获取用户的歌单列表
    getUserSongSheetList() {
      let url = this.$host1 + `home/userSongSheetList`;
      this.$axios
        .get(url, {
          params: {
            pageSize: 10000,
            page: 1,
            user_id: this.userinfo.user_id
          }
        })
        .then(res => {
          //console.log(res)
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.list = resData.data.list;
            } else {
              this.list = [];
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 关闭添加到歌单的模态框
    closeCollectModel() {
      this.isShowCollect = false;
    },
    mianBox() {},
    // 关闭模态框
    closeModel() {
      this.isShow = false;
      this.$emit("fn", this.isShow);
    },
    // 判断是否在微信中打开
    is_weixn() {
      var ua = navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == "micromessenger") {
        // 表示在微信里面
        this.leaderModal = true;
        return true;
      } else {
        return false;
      }
    },
    // 选择歌曲操作类型
    optionCheck(id) {
      console.log(id, "---id");
      if (id == 1) {
        // 表示当前点击的是下一首播放
        this.musicMes = JSON.parse(window.localStorage.getItem("musicMes"));
        let musicId = this.musicMes.music_id; //表示本地返回的正在播放音乐的id，
        console.log(this.musicObject, "--musicObject");
        console.log(musicId, "---musicId");
        if (this.musicObject.music_id == musicId) {
          // 表明当前添加到下一首播放的歌曲就是正在播放的歌曲
          this.AlertWin("添加成功");
        } else {
          // 先判断一下当前播放列表有没有数据
          let number = JSON.parse(window.localStorage.getItem("playingList"))
            .length;
          if (number == 0) {
            console.log("-----"); //没有播放信息
            this.checkedList.push(this.musicObject);
            this.store.playingList = this.checkedList;
            window.localStorage.setItem(
              "playingList",
              JSON.stringify(this.checkedList)
            );
            this.closeModel();
            this.AlertWin("添加成功");
          } else {
            this.playingList = JSON.parse(
              window.localStorage.getItem("playingList")
            );
            this.playingList.map((item, index) => {
              if (item.music_id == this.musicObject.music_id) {
                // 如果要添加的歌曲已经在播放列表里面则先删除调播放列表里面的，然后再添加进去
                this.playingList.splice(index, 1);
              }
            });
            this.playingList.map((item, index) => {
              if (item.music_id == musicId) {
                this.result = index + 1; //取出当前播放音乐在播放列表的下标   加一表示位置
              }
            });
            // console.log(this.playingList, "====this.playingList--after");
            if (this.result == this.playingList.length) {
              // 表明当前正在播放的歌曲位于数组的最后一位
              this.playingList.unshift(this.musicObject);
            } else {
              this.playingList.splice(this.result, 0, this.musicObject);
            }
            this.closeModel();
            this.AlertWin("添加成功");
            this.store.playingList = this.playingList;
            window.localStorage.setItem(
              "playingList",
              JSON.stringify(this.playingList)
            );
          }
        }
      } else if (id == 2) {
        // 表明当前点击的是收藏到歌单
        this.isShowCollect = true;
      } else if (id == 4) {
        // 表明当前点击的是评论
        this.closeModel();
        this.$router.push({
          path: "/index/comment",
          query: { status: 5, id: this.musicObject.music_id }
        });
      } else if (id == 5) {
        // 表明当前点击的是分享
        console.log(this.musicObject, "---this.musicObject");
        this.is_weixn();
        // 记录进入app时的url
        if (typeof window.entryUrl === "undefined" || window.entryUrl === "") {
          window.entryUrl = location.href.split("#")[0];
        }
        if (this.isapp == 0) {
          this.wxshare.getWxmessage(
            `分享歌曲` + this.musicObject.music_name,
            `歌手:` + this.musicObject.singer_name,
            /(Android)/i.test(navigator.userAgent)
              ? location.href.split("#")[0]
              : window.entryUrl,
            this.musicObject.background,
            location.href
          );
        } else if (this.isapp == 1) {
          var sendData = {
            type: "1",
            data: {
              title: this.musicObject.music_name,
              description: this.musicObject.singer_name,
              shareimg: this.musicObject.background,
              share_url: location.href
            }
          };
          window.postMessage(JSON.stringify(sendData));
        }
      } else if (id == 6) {
        // 表示点击的是歌手‘
        // /index/song-sheet?id=2&sheetStatus=2&isSinger=true
        this.$router.push({
          path: "/index/song-sheet",
          query: {
            id: this.musicObject.singer_id,
            sheetStatus: 2,
            isSinger: true
          }
        });
        this.$forceUpdate();
        this.reload();
        console.log(
          this.musicObject.singer_id,
          "----this.musicObject.singer_id"
        );
      } else if (id == 7) {
        // 表示点击的是专辑‘
        this.$router.push({
          path: "/index/album-detail",
          query: { id: this.musicObject.music_list_id }
        });
      }
    }
  },
  created() {
    this.getUserInfo();
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100000;
  .song-more {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    .main-mes {
      width: 100%;
      position: absolute;
      left: 0;
      bottom: 0;
      padding-top: 0.32rem;
      box-sizing: border-box;
      border-radius: 0.2rem 0.2rem 0 0;
      background-color: #f5f5f5;
      .song-mes {
        width: 100%;
        height: 1.53rem;
        display: flex;
        padding: 0 0.23rem;
        box-sizing: border-box;
        border-bottom: 0.01rem solid #dddddd;
        .left {
          margin-top: 0.12rem;
          img {
            width: 0.97rem;
            height: 0.97rem;
            border-radius: 0.12rem;
            margin-right: 0.24rem;
          }
        }
        .right {
          margin-top: 0.13rem;
          .song-name {
            font-weight: 500;
            color: rgba(68, 68, 68, 1);
            margin-bottom: 0.14rem;
          }
          .singer-name {
            font-weight: 500;
            color: rgba(153, 153, 153, 1);
          }
        }
      }
    }

    .option-list {
      padding: 0 0.24rem;
      box-sizing: border-box;
      .items {
        width: 100%;
        height: 1rem;
        line-height: 1rem;
        padding-left: 0.37rem;
        box-sizing: border-box;
        border-bottom: 0.01rem solid #dddddd;
        .left {
          display: flex;
          align-items: center;
          .img {
            width: 0.4rem;
            height: 0.4rem;
            img {
              width: 100%;
            }
          }
          .text {
            margin-left: 0.29rem;
          }
          .vip {
            margin-left: 0.12rem;
            width: 0.32rem;
            height: 0.32rem;
            img {
              width: 100%;
            }
          }
        }
      }
    }
    .option-list:last-child {
      .items {
        border: none;
      }
    }
  }

  // 收藏到歌单模态框
  .collectModel {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    .add-box {
      position: absolute;
      width: 90%;
      // height: 5rem;
      background-color: #fff;
      left: 50%;
      top: 25%;
      border-radius: 0.14rem;
      transform: translate(-50%);
      padding: 0.24rem;
      box-sizing: border-box;
      .add {
        display: flex;
        align-items: center;
        margin-bottom: 0.24rem;
        .img {
          position: relative;
          width: 1rem;
          height: 1rem;
          margin-right: 0.25rem;
          background-color: rgba(248, 248, 248, 1);
          img {
            width: 0.3rem;
            height: 0.3rem;
            position: absolute;
            left: 50%;
            top: 35%;
            transform: translate(-50%);
          }
          span {
          }
        }
      }

      .list {
        width: 100%;
        height: auto;
        .list-item {
          height: 1rem;
          margin-bottom: 0.12rem;
          padding-bottom: 0.12rem;
          box-sizing: border-box;
          .center-col {
            width: 100%;
            height: 100%;
            .left {
              width: auto;
              min-width: 1rem;
              max-width: 1.5rem;
              .yuan {
                width: 0.36rem;
                height: 0.36rem;
                margin-right: 0.2rem;
                p {
                  width: 0.2rem;
                  height: 0.03rem;
                }
              }
              img {
                width: 0.88rem;
                height: 0.88rem;
              }
            }
            .right {
              margin-left: 0.2rem;
              height: 0.88rem;
            }
          }
        }
      }
    }
  }

  //新建歌单模态框
  .addmusic-list {
    position: fixed;
    background-color: rgba(0, 0, 0, 0.8);
    z-index: 199999;
    width: 100%;
    height: 100vh;
    top: 0;
    left: 0;
    .contents {
      width: 100%;
      height: 5.3rem;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 0.15rem 0.15rem 0 0;
      .top {
        height: 1rem;
        border-bottom: 0.01rem solid #e9e9e9;
      }
      .setmusic {
        padding: 0.2rem 0.24rem;
        input {
          width: 100%;
          height: 0.6rem;
          margin-top: 0.5rem;
          border: 0.015rem solid #999;
          border-radius: 0.12rem;
          text-indent: 0.2rem;
          //background-color: greenyellow;
        }
        .center-col {
          width: 100%;
          height: 0.6rem;
          margin-top: 0.2rem;
          //background-color: greenyellow;
          .yuan {
            width: 0.4rem;
            height: 0.4rem;
            margin-right: 0.2rem;
            border: 0.01rem solid #e9e9e9;
            p {
              width: 0.3rem;
              height: 0.3rem;
              border-radius: 50%;
              background-color: #fff;
            }
          }
          .active {
            border: 0.01rem solid #f44349;
            p {
              background-color: #f44349;
            }
          }
        }
      }
    }
  }
}
</style>
